//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "framework\fn_loadMissions.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;
private ["_current_world","_use_external_missions","_static_missions_count","_static_missions_path","_external_missions_count","_external_missions_path","_missions","_missions_headers"
        ,"_mission_file","_header_file","_header","_mission","_i","_j","_chapters","_chapter","_name"];
if (!isServer) exitWith {ERROR("Not the server")};
_current_world = ["CURRENT_WORLD",GVAR(current_world)] call FFUNC(getFrameworkValue);
_use_external_missions = ["USE_EXTERNAL_MISSIONS",tmf_use_external_missions] call FFUNC(getFrameworkValue);

_static_missions_count = ["STATIC_MISSIONS_COUNT",0] call FFUNC(getFrameworkValue);
_static_missions_path = ["STATIC_MISSIONS_PATH",""] call FFUNC(getFrameworkValue);
_external_missions_count = ["EXTERNAL_MISSIONS_COUNT",0] call FFUNC(getFrameworkValue);
_external_missions_path = ["EXTERNAL_MISSIONS_PATH",""] call FFUNC(getFrameworkValue);

_missions_headers = [];
_missions = [];

if (_static_missions_count > 0) then {
	for [{_i = 1}, {_i <= _static_missions_count},{_i = _i + 1}] do {
		_header_file = format ["%1m%2.sqf",_static_missions_path,_i];
		_header = compile preprocessFileLineNumbers loadFile _header_file;
		_mission = [];
		_chapters = [_header,"CHAPTERS"] call CFUNC(pairGet);
		_worlds = [_header,"WORLDS"] call CFUNC(pairGet);
		_status = [_header,"STATUS"] call CFUNC(pairGet);
		_name = [_header,"NAME"] call CFUNC(pairGet);
		TRACE_1("Loaded",ARR_3(_name,_chapters,_status));
		if (_status > 0 && _chapters > 0 && ((count _worlds <= 0) || (_current_world in _worlds))) then {
			for [{_j = 1},{_j <= _chapters},{_j = _j + 1}] do {
				_mission_file = format ["%1m%2\c%3.sqf",_static_missions_path,_i,_j];
				_chapter = compile preprocessFileLineNumbers loadFile _mission_file;
				_mission set [count _mission, _chapter];
			};
			_header = [_header,"STATIC",true] call CFUNC(pairSet);
			_header = [_header,"FILE_INDEX",_i] call CFUNC(pairSet);
			_missions_headers set [count _missions_headers, _header];
			_missions set [count _missions, _mission];
			JIPSETVAR(QGVAR(FRAMEWORK_MISSIONS_HEADERS),_missions_headers);
			OSETVAR(GVAR(mission_store),QGVAR(FRAMEWORK_MISSIONS_HEADERS),_missions_headers);
			OSETVAR(GVAR(mission_store),QGVAR(FRAMEWORK_MISSIONS_CONTENT),_missions);
		};
	};
};
if (_use_external_missions > 0) then {
	_header_file = format ["%1list.sqf",_external_missions_path];
	_external_missions_count = compile preprocessFileLineNumbers loadFile _header_file;
	
	for [{_i = 1}, {_i <= _external_missions_count},{_i = _i + 1}] do {
		_header_file = format ["%1m%2.sqf",_external_missions_path,_i];
		_header = compile preprocessFileLineNumbers loadFile _header_file;
		_mission = [];
		_chapters = [_header,"CHAPTERS",1] call CFUNC(pairGet);
		_worlds = [_header,"WORLDS",[]] call CFUNC(pairGet);
		_status = [_header,"STATUS",1] call CFUNC(pairGet);
		_name = [_header,"NAME","NO NAME"] call CFUNC(pairGet);
		TRACE_1("Loaded",ARR_3(_name,_chapters,_status));
		if (_status > 0 && _chapters > 0 && ((count _worlds <= 0) || (_current_world in _worlds))) then {
			for [{_j = 1},{_j <= _chapters},{_j = _j + 1}] do {
				_mission_file = format ["%1m%2\c%3.sqf",_external_missions_path,_i,_j];
				_chapter = compile preprocessFileLineNumbers loadFile _mission_file;
				_mission set [count _mission, _chapter];
			};
			_header = [_header,"STATIC",false] call CFUNC(pairSet);
			_header = [_header,"FILE_INDEX",_i] call CFUNC(pairSet);
			_missions_headers set [count _missions_headers, _header];
			_missions set [count _missions, _mission];
			JIPSETVAR(QGVAR(FRAMEWORK_MISSIONS_HEADERS),_missions_headers);
			OSETVAR(GVAR(mission_store),QGVAR(FRAMEWORK_MISSIONS_HEADERS),_missions_headers);
			OSETVAR(GVAR(mission_store),QGVAR(FRAMEWORK_MISSIONS_CONTENT),_missions);
		};
	};
};
TRACE_1("End",count _missions_headers);
TMFPROFILERSTOP;
